var act = '';
var showAccountColumns;

function accountsShowInfo(id_account) {
    $('body').append('<div id="account_info"></div>');
    $('body').append('<div id="account_info_buttons" style="display:none;" align="right"></div>');
    $('#account_info_buttons').append('<a href="#" rel="" iconCls="icon-cancel" onclick="accountsInfoOnClose(); return false;">' + getTranslation('Close') + '</a>');
    $('#account_info_buttons a').linkbutton();
    $('#account_info').append('<div id="account_info_datagrid"></div>');
    accountsDrawInfo(id_account);
}

function accountsInfoOnClose() {
    $('#account_info').dialog('destroy');
}
//init
$(function() {
    var rows =[];
    $.ajax({
		url:"/Home/User/getZhanghuLog",
		type: 'get',
	    dataType: 'json',
		success:function(result){
			// alert(1);
			rows=result.accounts;
			$('#accounts').datagrid({
				title: getTranslation('common_private_accounts'),
		        fitColumns: true,
		        fit: true,
		        singleSelect: true,
		        columns: [[{
		            field: 'id',
		            title: getTranslation('ID'),
		            width: 100,
		            formatter: function(value) {
		                return value;
		            }
		        },
		        {
		            field: 'a_n',
		            title: getTranslation('Name'),
		            width: 100
		        },
		        {
		            field: 'cursname',
		            title: getTranslation('Currency code'),
		            width: 100,
		            formatter: function(value) {
		                return value;
		            }
		            /*formatter: function(value) {
		            	if(value==850){
		            		return 'CNY: CNY';
		            	}else if(value==840){
		            		return 'BTC: Bitcoin';
		            	}else{
		            		return 'BCH: Bitcoin';
		            	}		                
		            }*/
		        },
		        {
		            field: 'real_bankname',
		            title: getTranslation('Bank'),
		            width: 100,
		            formatter: function(value) {
		                return value;
		            }
		        },
		        {
		            field: 'a_num',
		            title: getTranslation('Card/Account Number'),
		            width: 100
		        },
		        {
		            field: 'name',
		            title: getTranslation('Account Holder'),
		            width: 100
		        }]],
		        onLoadSuccess: function(data) {
		            if (parseInt(data.total, 10) && _cs == 0) {
		                $('#add_account, #edit_account, #remove_account').linkbutton('disable');
		            }
		        },
		        toolbar: [{
		            id: 'add_account',
		            text: getTranslation('Add'),
		            iconCls: 'icon-add',
		            handler: function() {
		                act = 'ins';
		                createLayoutNew(0);
	                	$.ajax({
	                		 url: baselocation+'/private_accounts/add/',
	        				type: 'get',
	        				success:function(result){
	        					  $("#token").val(result.token);
	        				}
	        			})
		                return false;
		            }
		        },
		        {
		            id: 'edit_account',
		            text: getTranslation('Edit'),
		            iconCls: 'icon-edit',
		            handler: function() {
		            	
		                var sel = $('#accounts').datagrid('getSelections');
		                console.log(sel);
		                if (typeof(sel[0]) != 'undefined') {
		                    createLayoutNew(sel[0].id);
		                	$.ajax({
		                		 url: baselocation+'/private_accounts/edit/',
		                		 type: 'get',
		                		 data:{id:sel[0].id},
		                		 success:function(result){
		                			 $('#addacc_bank_type').val(result.a_type);
		                			 if(result.a_type==840){
		                				 $("._requisite_line").hide();
		                				 $('#con_bch_address').hide();
			                			 $('#con_bit_address').show();
			                			 $("#bit_address").val(result.a_num);
		                			 }else if(result.a_type==860){
		                				 $("._requisite_line").hide();
			                			 $('#con_bch_address').show();
			                			 $("#bch_address").val(result.a_num);
			                			 $('#combo_btype').val('BitCoin Cash(BCH)');
			                			 $('#b_840').removeClass("combobox-item-selected");	
			                			 $('#b_850').removeClass("combobox-item-selected");	
			        					 $('#b_860').addClass("combobox-item-selected");
		                			 }else{		        
			                			 $("._requisite_line").show();
			                			 $('#con_bit_address').hide();
			                			 $('#con_bch_address').hide();
			                			 $("#addacc_bank_name").val(result.a_bn);
			        					 $("#accountNumber").val(result.a_num);
			        					 $("#firstName").val(result.a_fn);
			        					 $('#combo_btype').val(getTranslation('Bank'));
			        					 $('#b_840').removeClass("combobox-item-selected");	
			        					 $('#b_860').removeClass("combobox-item-selected");
			        					 $('#b_850').addClass("combobox-item-selected");
		                			 }
		                			 $('#con_addacc_bank_type').show();
		                			 $("#accountName").val(result.a_n);
		        					 $("#token").val(result.token);
		        				}
		        			})		                  
		                }
		                return false;
		            }
		        },
		        {
		            id: 'remove_account',
		            text: getTranslation('Remove'),
		            iconCls: 'icon-remove',
		            handler: function() {
		                var selected = $('#accounts').datagrid('getSelections');
		                if (typeof(selected[0]) != 'undefined') {
		                    showConfirm(getTranslation('Are you sure you want to delete record') + '?',
		                    function(result) {
		                    	if (result) {
			                    	loadingShow();
			                    	$.ajax({
				                		url:'/Home/User/deleteZhanghu',
				        				type: 'post',
				        				data:{id:selected[0].id,token:$('#account_del_token').val()},
				        				success:function(result){			        		
			                                    if (result.status==1) {
			                                    	location.reload();
			                                 		loadingHide();
			                                    }
			                                    else if (result.status == 1200) {
			                                    	showAlert('No permision');
			                                    } 
				        				}
				        			})
		                    	}		           
		                    });
		                }
		                return false;
		            }
		        }]
		    }).datagrid("loadData", {rows: rows});
			 $('#add_account span, #edit_account span').css('font-size', '20px');
		}
	});	
  
    $('#wizard_field_account_form_st_number').die().live('blur',
    function() {
        vValidateStNumber($(this), $('#wizard_field_account_form_number'));
    });
    $('#to_text').text(getTranslation('accounts_private_accounts_header_message'));
    
    $(".combo-arrow").die().live("click",function(){
	    var $par=$(this).parents(".field_combo");
		var $obj=$(".field_comboBox",$par);
		$obj.toggle();							   
	    $(".combobox-item",$obj).off("click").on("click",function(){
				$(".combobox-item",$obj).removeClass("combobox-item-selected");												  
				$(this).addClass("combobox-item-selected");
				if($(this).attr('data-id')==850){
					$("._requisite_line").show();
					$('#con_bit_address').hide();
					$('#con_bch_address').hide();
				}else if($(this).attr('data-id')==840){
					$("._requisite_line").hide();
					$('#con_bit_address').show();
				}else if($(this).attr('data-id')==860){
					$("._requisite_line").hide();
					$('#con_bit_address').hide();
					$('#con_bch_address').show();
				}
				$('#con_addacc_bank_type').show();
				$('#addacc_bank_type').val($(this).attr('data-id'));
				$(".validatebox-text",$par).val($(this).html());												  
				$obj.hide();													  
		})
 })
});
function createLayoutNew(i18) {
    $('#ml').layout('add', {
        region: 'east',
        width: 700,
        title: i18 ? getTranslation('Edit account') : getTranslation('Add account'),
        split: false,
        collapsed: true
    });
    $('<div style="margin-top: 10px;" id="account_form"></div>').appendTo($('#ml').find('.layout-panel-east .layout-body'));
    destroyWizards(function() {
        $('#account_form').wizard('private_account_form_new', {
            'load': function() {},
            'ok': function(data) {
                $('#accounts').datagrid('reload');
                $('#ml').layout('remove', 'east');
                if (getTranslation(data.mess)) {
                    $.messager.alert(getTranslation('Result'), getTranslation(data.mess), data.mess_type);
                }
            },
            'nextStep': function() {
                $('#accounts').datagrid('reload');
                $('#ml').layout('remove', 'east');
            },
            'onValidate': function() {
                vValidateStNumber($('#wizard_field_account_form_number'), $('#wizard_field_account_form_st_number'));
            },
            'cancel': function(data) {
                $('#ml').layout('remove', 'east');
            },
            'end': function(data) {
                $('#ml').layout('remove', 'east');
                $('#accounts').datagrid('reload');
            },
            'i18': i18
        });
        $("#i18").val(i18);
        $('#ml').layout('expand', 'east');
    });
}
$('._request_sms').die().live('click',
  function() {
	 var $win = $('<div style="padding: 10px;">' + '<table><tr><td>' + '	<img class="capcha" src="/c?' + (100000 * Math.random() + 10000) + '" style="border:1px solid lightslategrey;" /><br />' + '	<a class="recapcha" style="text-decoration:none;border-bottom:1px dashed #000;font-size:11px;cursor:pointer;">' + getTranslation('refresh_code') + '</a>' + '</td>' + '<td>' + '<label style="line-height:16px;margin:0 0 20px 0;">' + getTranslation('enter_captcha') + ':</label><br />' + '<input class="capcha" name="capcha" value="" type="text" style="width:100px;" />' + '</td></tr></table>' + '<div style="text-align: center;"><a class="_ok" >' + getTranslation('Ok') + '</a>&nbsp;<a class="_cancel" >' + getTranslation('Cancel') + '</a></div>' + '</div>');
	 $win.appendTo('body');
	loadingShow();
	setTimeout(function(){
		loadingHide();
	},1000)
	$.ajax({
		url:baselocation+"/private_accounts/getcode/",
		type: 'get',
		success:function(result){
			if(result.code==1){
				showAlert(getTranslation('Email is sent'));
			}else if(result.code==2){
				showAlert(getTranslation('error_code_sent_please_wait'));
			}else{
				showAlert(getTranslation('error_send_email'));
			}
	}
})
	
})
$(".icon-ok").die().live('click',
        function() {
			var accountNumber=$("#accountNumber").val();
			var accountName=$("#accountName").val();
			var firstName=$("#firstName").val();	
			var reg =/^(?=.*\d)[a-z\d]{10,40}$/i;  
			var mailcode = $('._apply_sms').val();
			var addacc_bank_type = $("#addacc_bank_type").val();
			//alert(addacc_bank_type);return;
			var addacc_bank_name = $("#addacc_bank_name").val();
			if(mailcode.length==0){
				showError(getTranslation('Enter_verification_Code'));
				return;
			}
			if(addacc_bank_type==840){
				addacc_bank_name='USDT';
				accountNumber=$("#bit_address").val();
				if(accountName.length==0){
					showError(getTranslation('Enter_account_name'));
					return;
				}
				/*if(!reg.test(accountNumber)){
				showError(getTranslation('Invalid_Bitcoin_address'));
				return;
				};*/
			}/*else if(addacc_bank_type==860){
				addacc_bank_name='BCH BitCoin';
				accountNumber=$("#bch_address").val();
				if(accountName.length==0){
					showError(getTranslation('Enter_account_name'));
					return;
				}
			}*/
			
		var i18=$('#i18').val();
		if(i18==0){
			var url="/Home/User/addZhanghu";
		}else{
			url="/Home/User/editZhanghu";
		}
		$.ajax({
				url:url,
				type: 'POST',
				data:{id:i18,accountName:accountName,accountNumber:accountNumber,firstName:firstName,token:$('#token').val(),email_code:mailcode,addacc_bank_type:addacc_bank_type,addacc_bank_name:addacc_bank_name},
				success:function(result){
					if(result.status==1){
						location.reload();
					}else{
						showError(getTranslation('系统繁忙'));
					}
					/*if(result.code==0){
						showError(getTranslation('retry_after'));
					}else if(result.code==2){
						showAlert(getTranslation('Wrong_code_Left_tries')+result.t);
					}else if(result.code==1){
						location.href=baselocation+"/private_accounts/";
					}else if(result.code==4){
						showAlert(getTranslation('Max_allowed_attempts_used'));
					}*/
				}
			})	
   });
$(".icon-cancel").die().live('click',function() {
			location.reload();
   		});